//
//  MYGHeadDetailCommentCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/7.
//

import UIKit
import QMUIKit

class MYGHeadDetailCommentCell: UITableViewCell {
    // MARK: - Property
    var model: MYGFindDetailComment? {
        didSet {
            guard let model = model else {
                return
            }
            
            layerUI()
            avatar.sd_setImage(with: URL(string: model.doctor.avatar))
            nameLabel.text = model.doctor.name
            contentLabel.text = model.content
            timeButton.setTitle("\(model.created_at.toDate("yyyy-MM-dd HH:mm:ss")?.toString(.custom("yyyy-MM-dd")) ?? "")", for: .normal)
            greatButton.setTitle("\(model.likes)", for: .normal)
            greatButton.isSelected = model.is_like == 1
            
            greatButton.isHidden = model.isSecond
        }
    }
    
    var likeAction: (()->Void)?
    var commentAction:(()->Void)?
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        contentView.backgroundColor = .white
        contentView.addSubview(avatar)
        contentView.addSubview(stackView)
        contentView.addSubview(lineView)
        contentView.addSubview(greatButton)
        
        stackView.addArrangedSubviews([nameLabel, contentLabel, timeButton])

        layerUI()
    }
    
    private func layerUI() {
        let isSecond = model?.isSecond ?? false
        avatar.snp.remakeConstraints { make in
            if isSecond {
                make.left.equalToSuperview().offset(60)
                make.top.equalToSuperview().offset(8)
                make.width.height.equalTo(28)
            } else {
                make.left.equalToSuperview().offset(15)
                make.top.equalToSuperview().offset(15)
                make.width.height.equalTo(40)
            }
        }
        
        stackView.snp.remakeConstraints { make in
            if isSecond {
                make.left.equalTo(avatar.snp.right).offset(6)
                make.right.equalToSuperview().inset(20)
            } else {
                make.left.equalTo(avatar.snp.right).offset(10)
                make.right.equalTo(greatButton.snp.left).offset(-10)
            }
            make.top.equalTo(avatar.snp.top)
            make.bottom.equalToSuperview().inset(8)
        }
        
        greatButton.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(17)
            make.centerY.equalToSuperview()
        }
        
        lineView.snp.remakeConstraints { make in
            if isSecond {
                make.left.equalToSuperview().offset(60)
                make.bottom.right.equalToSuperview()
            } else {
                make.right.bottom.equalToSuperview()
                make.left.equalToSuperview().offset(65)
            }
            make.height.equalTo(0.5)

        }
        
        greatButton.setContentHuggingPriority(.required, for: .horizontal)
        greatButton.setContentCompressionResistancePriority(.required, for: .horizontal)
    }
    
    // MARK: - lazy load
    private lazy var avatar: UIImageView = {
        let imgV = UIImageView()
        imgV.sd_setImage(with: URL(string: "https://img0.baidu.com/it/u=440666296,1667611490&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1709917200&t=8c3514d50769bb0e0e0fdf4867fc1df1"))
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 20
        return imgV
    }()
    
    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .vertical
        stackV.spacing = 3
        stackV.alignment = .leading
        return stackV
    }()

    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(13)
        label.textColor = .rgba(102, 102, 102, 1)
        return label
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .rgba(51, 51, 51, 1)
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var timeButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitleColor(.rgba(204, 204, 204, 1), for: .normal)
        btn.titleLabel?.font = .regular(12)
        btn.spacingBetweenImageAndTitle = 6
        btn.imagePosition = .right
        btn.setImage(UIImage(named: "comment"), for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238, 1)
        return view
    }()
    
    private lazy var greatButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "great"), for: .normal)
        btn.setImage(UIImage(named: "great_sel"), for: .selected)
        
        btn.setTitleColor(.rgba(136, 136, 136, 1), for: .normal)
        btn.titleLabel?.font = .regular(12)
        
        btn.spacingBetweenImageAndTitle = 4
        btn.imagePosition = .left
        
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    @objc
    private func clickButton(_ btn: UIButton) {
        if btn == greatButton {
            self.likeAction?()
        } else if btn == timeButton {
            self.commentAction?()
        }
    }
}
